package com.company.hrmadmin.modules.controller;

import com.company.hrmadmin.common.constant.GlobalConstant;
import com.company.hrmadmin.common.constant.Result;
import com.company.hrmadmin.modules.entity.dto.login.ResetPasswordDTO;
import com.company.hrmadmin.modules.entity.dto.login.UserLoginDTO;
import com.company.hrmadmin.modules.entity.dto.profile.UpdateAddressDTO;
import com.company.hrmadmin.modules.entity.dto.profile.UpdateCollegeDTO;
import com.company.hrmadmin.modules.entity.dto.profile.UpdatePasswordDTO;
import com.company.hrmadmin.modules.entity.dto.profile.UpdateProfileDTO;
import com.company.hrmadmin.modules.entity.vo.IndexPanelVO;
import com.company.hrmadmin.modules.entity.vo.UserProfileVO;
import com.company.hrmadmin.modules.service.LoginAuthorService;
import com.company.hrmadmin.modules.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户控制层
 *
 * @author Song, Wei
 * Date: 2022/3/18 15:29
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private LoginAuthorService loginAuthorService;
    @Autowired
    private UserService userService;

    @RequestMapping(path = "/login", method = RequestMethod.POST)
    public Result<?> login(@RequestBody UserLoginDTO dto) {
        log.info("用户登录，参数：{}", dto);
        return Result.success(loginAuthorService.login(dto));
    }

    @RequestMapping(path = "/info", method = RequestMethod.GET)
    public Result<?> getUserInfo(String token) {
        log.info("获取用户详情页面，Token参数：{}", token);
        return Result.success(loginAuthorService.getUserInfo(token));
    }

    @RequestMapping(path = "/register", method = RequestMethod.POST)
    public Result<?> register(@RequestBody UserLoginDTO dto) {
        log.info("用户注册，参数：{}", dto);
        // TODO: 调用用户注册的初始化方法
        return Result.success();
    }

    @RequestMapping(path = "/updatePassword", method = RequestMethod.POST)
    public Result<?> updatePassword(@RequestBody UpdatePasswordDTO dto) {
        log.info("用户更新密码，参数：{}", dto);
        return Result.success(loginAuthorService.updatePassword(dto));
    }

    @RequestMapping(path = "/updateProfile", method = RequestMethod.POST)
    public Result<?> updateProfile(@RequestBody UpdateProfileDTO dto) {
        log.info("用户更新个人信息，参数：{}", dto);
        return Result.success(loginAuthorService.updateProfile(dto));
    }

    @RequestMapping(path = "/updateAvatar", method = RequestMethod.POST)
    public Result<?> updateAvatar(@RequestBody(required = false) MultipartFile file,
                                  @RequestHeader(GlobalConstant.TOKEN_NAME) String token) {
        log.info("用户更新个人头像，参数：{}", file);
        return Result.success(userService.uploadAvatar(file, token));
    }

    @RequestMapping(path = "/avatar/{picture}", method = RequestMethod.GET)
    public Result<?> getAvatar(@PathVariable("picture") String fileName, HttpServletResponse response) {
        log.info("用户获取个人头像，参数：{}", fileName);
        userService.getAvatar(fileName, response);
        return Result.success();
    }

    @RequestMapping(path = "/profile", method = RequestMethod.GET)
    public Result<UserProfileVO> getUserProfileVOResult(Integer userId) {
        log.info("获取用户详情页，用户ID参数：{}", userId);
        return Result.success(userService.getUserProfile(userId));
    }

    @RequestMapping(path = "/profile/edit/college", method = RequestMethod.POST)
    public Result<Integer> editUserEducation(@RequestBody UpdateCollegeDTO dto) {
        log.info("用户修改个人教育背景，参数：{}", dto);
        return Result.success(userService.setUserCollege(dto));
    }

    @RequestMapping(path = "/profile/edit/address", method = RequestMethod.POST)
    public Result<Integer> editUserAddress(@RequestBody UpdateAddressDTO dto) {
        log.info("用户修改个人地址信息，参数：{}", dto);
        return Result.success(userService.setUserAddress(dto));
    }

    @RequestMapping(path = "/generator/loginAccount", method = RequestMethod.GET)
    public Result<Integer> generatorLoginAccount() {
        log.info("生成所有登录账号");
        return Result.success(loginAuthorService.generatorLoginAccount());
    }

    @RequestMapping(path = "/index/panel", method = RequestMethod.GET)
    public Result<IndexPanelVO> getIndexPanel(Integer userId) {
        log.info("获得首页Panel信息页，参数：{}", userId);
        return Result.success(userService.getIndexPanel(userId));
    }

    @RequestMapping(path = "/send/verify", method = RequestMethod.GET)
    public Result<?> sendForgetVerify(String email) {
        log.info("发送忘记密码的验证码，参数：{}", email);
        return Result.success(userService.sendVerify(email));
    }

    @RequestMapping(path = "/reset/password", method = RequestMethod.POST)
    public Result<Integer> resetPassword(@RequestBody ResetPasswordDTO dto) {
        log.info("用户重置密码，参数：{}", dto);
        return Result.success(userService.resetPassword(dto));
    }

    @RequestMapping(path = "/logout", method = RequestMethod.POST)
    public Result<?> logout(@RequestBody String token) {
        log.info("用户登出，参数：{}", token);
        loginAuthorService.logout(token);
        return Result.success();
    }

}
